package com.lisen.codegenerator.util;

import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;

public class ClassDescriptorImporter {

	private ClassDescriptor classMain;
	private String sheetname;
	private String sheetcnname;

	public ClassDescriptorImporter() {
		classMain = new ClassDescriptor();
	}

	public void parse(Document doc) {
		Element root = doc.getRootElement();
		// 中文名
		this.sheetcnname = root.attributeValue("sheetcnname");
		// 表名
		this.sheetname = root.attributeValue("name");
		Iterator itmain = root.elementIterator("main");
		while (itmain.hasNext()) {
			Element mainEl = (Element) itmain.next();
			// 主属性
			classMain.setName(mainEl.attributeValue("name"));
			classMain.setTaskName(mainEl.attributeValue("taskName"));
			// property属性
			parseProperty(classMain, mainEl);
		}
	}

	private void parseProperty(ClassDescriptor classMain, Element mainEl) {
		Iterator itproperties = mainEl.elementIterator("property");
		while (itproperties.hasNext()) {
			Element propertyEl = (Element) itproperties.next();
			AttributeDescriptor ad = new AttributeDescriptor();
			ad.setColumn(propertyEl.attributeValue("column"));
			ad.setInputtype(propertyEl.attributeValue("inputtype"));
			ad.setLength(GeneratorUtility.nullObject2int(propertyEl
					.attributeValue("length")));
			ad.setName(propertyEl.attributeValue("name"));
			ad.setType(propertyEl.attributeValue("type"));
			ad.setTitle(propertyEl.attributeValue("title"));
			ad.setNotnull(GeneratorUtility.nullObject2String(propertyEl
					.attributeValue("notnull")));
			classMain.add(ad);
		}
	}

	public ClassDescriptor getClassMain() {
		return classMain;
	}

	public void setClassMain(ClassDescriptor classMain) {
		this.classMain = classMain;
	}

	public String getSheetname() {
		return sheetname;
	}

	public void setSheetname(String sheetname) {
		this.sheetname = sheetname;
	}

	public String getSheetcnname() {
		return sheetcnname;
	}

	public void setSheetcnname(String sheetcnname) {
		this.sheetcnname = sheetcnname;
	}

}
